package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"main.go/database"
	"main.go/model"
	"net/http"
	"strconv"
)

//查找
func Find_house(db *gorm.DB, request model.House) bool {
	var house model.House
	db.Where("house_id = ?", request.House_id).First(&house)
	if house.House_id != "" {
		fmt.Println("查找成功")
		return true
	}
	fmt.Println("查找失败")
	return false
}

//插入
func Insert_house(db *gorm.DB, request model.House) bool {
	if Find_house(db, request) == true {
		fmt.Println("该仓库已存在，不能插入")
		return false
	}
	db.Create(&request)
	fmt.Println("插入成功")
	return true
}

//删除
func Delete_house(db *gorm.DB, request model.House) bool {
	if Find_house(db, request) == false {
		fmt.Println("不存在该仓库，无法删除")
		return false
	}
	db.Where("house_id = ?", request.House_id).Delete(&request)
	fmt.Println("删除成功")
	return true
}

//更新
func Update_house(db *gorm.DB, request model.House) bool {
	if Find_house(db, request) == false {
		fmt.Println("无该仓库，无法更新")
		return false
	}
	db.Model(&request).Where("house_id = ?", request.House_id).Updates(
		model.House{
			House_id:            request.House_id,
			House_port_company:  request.House_port_company,
			House_goods_company: request.House_goods_company,
			House_rate:          request.House_rate,
		},
	)
	fmt.Println("更新成功")
	return true
}
func Web_show_house(c *gin.Context) {
	db := database.GetDB()
	var house []model.House
	result := db.Table("houses").Find(&house)
	if result != nil {
		fmt.Println("!!")
	}
	fmt.Println(house)
	c.JSON(http.StatusOK, gin.H{
		"code":  "200",
		"house": house,
		"msg":   "成功",
	})
}
func Web_get_house(c *gin.Context) {
	db := database.GetDB()
	var house1 model.House
	var house2 []model.House
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	house1.House_id = request["house_id"].(string)
	db.Where("house_id = ?", house1.House_id).First(&house2)
	if house2[0].House_id != "" {
		fmt.Println("成功查找到仓库")
		c.JSON(200, gin.H{
			"code":  "200",
			"house": house2, //传一个对象
			"msg":   "成功",
		})
	} else {
		c.JSON(404, gin.H{
			"code":  "404",
			"house": house2, //传一个空对象
			"msg":   "no",
		})
		fmt.Println("未找到该仓库")
	}
}
func Web_add_house(c *gin.Context) {
	db := database.GetDB()
	var house model.House
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	house.House_id = request["house_id"].(string)
	house.House_port_company = request["house_port_company"].(string)
	house.House_goods_company = request["house_goods_company"].(string)
	house.House_rate, _ = strconv.Atoi(request["house_rate"].(string))
	if Insert_house(db, house) {
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "成功",
		})
		fmt.Println("添加成功")
	} else {
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "添加失败",
		})
		fmt.Println("添加失败")
	}
}
func Web_delete_house(c *gin.Context) {
	db := database.GetDB()
	var house model.House
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	house.House_id = request["house_id"].(string)
	if Delete_house(db, house) {
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "成功",
		})
		fmt.Println("删除成功")
	} else {
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "删除失败",
		})
		fmt.Println("删除失败")
	}
}
func Web_update_house(c *gin.Context) {
	db := database.GetDB()
	var house model.House
	request := make(map[string]interface{})
	c.ShouldBind(&request)
	fmt.Println(request)
	house.House_id = request["house_id"].(string)
	house.House_port_company = request["house_port_company"].(string)
	house.House_goods_company = request["house_goods_company"].(string)
	house.House_rate, _ = strconv.Atoi(request["house_rate"].(string))
	if Update_house(db, house) {
		c.JSON(200, gin.H{
			"code": "200",
			"msg":  "成功",
		})
		fmt.Println("更新成功")
	} else {
		c.JSON(404, gin.H{
			"code": "404",
			"msg":  "更新失败",
		})
		fmt.Println("更新失败")
	}
}
